package mt.dummy;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


import mt.client.GraphicInterface;

import mt.client.MicroTraderClient;
import mt.comm.ClientComm;
import mt.comm.ClientSideMessage;
/**
 * This class initialized the client for MicroTrader
 * @author tom93d
 *
 */
public class MicroTraderClientDummy implements MicroTraderClient {

	private static JLabel username;
	private static JButton login;
	private static JTextField nickName;

	private ClientComm cc;
	
	private GraphicInterface graphicInterface;
	
	private static JFrame loginFrame;
	/*
	 * (non-Javadoc)
	 * @see mt.client.MicroTraderClient#start(mt.comm.ClientComm)
	 * 
	 * 
	 * Runnablae
	 * 
	 * recibe mensagem
	 * 
	 * fire() 
	 * 		
	 */
	
	JButton getLoginButton() {
		return login;
	}

	void setNickName(JTextField nickName) {
		MicroTraderClientDummy.nickName = nickName;
	}

	/**
	 * This method constructs the Login Frame and permit the client to login the Server.
	 * It creates a thread for the client for receiving messages (ClientRunnable).
	 * 
	 * @param clientComm
	 */
	private void buildGUILogin(ClientComm clientComm) {
		loginFrame = new JFrame();		
		JPanel panelLogin = new JPanel();

		panelLogin.add(username=new JLabel("Username"));
		panelLogin.add(nickName = new JTextField(15));
		panelLogin.add(login = new JButton("Login"));
		
		login.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				/*
				 * Get users ' list from the server and verify that the username is 
				 * 	if(nickname already connected) {
				 *			
				 *  }
				 *  
				 */			
				if(nickName.getText().equals("")) {
					JOptionPane.showMessageDialog(null,"Nickname cannot be empty!","ERROR",JOptionPane.INFORMATION_MESSAGE);
				} else {
				//	loginFrame.setVisible(false);
					/*
					 * Try to connect to the server
					 */
					try {
						//loginFrame.setVisible(false);
						loginFrame.dispose();
			
						graphicInterface = new GraphicInterface(cc, nickName.getText());
						//System.out.println("LoginFrame "+nickName.getText());
						cc.connect("127.0.0.1", nickName.getText());
				
						if(!cc.isConnected()) throw new Exception("Connection Failed");
						ClientRunnable client = new ClientRunnable();
						Thread t = new Thread(client);
						t.start();
					//	System.out.println("Connected.");
				//		t.join();
					} catch(Exception exception) {
						System.out.println(exception.getMessage());
					} 
				}
			}
		});

		loginFrame.add(panelLogin);
		loginFrame.setVisible(true);
		loginFrame.setTitle("Login");
		loginFrame.setSize(400, 400);
		loginFrame.setLocationRelativeTo(null);
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			
	}	
	
	/**
	 * This method calls buildGUILogin method.
	 */
	@Override
	public void start(ClientComm clientComm) {
		cc = clientComm;
		buildGUILogin(cc);
		/*
		 * Done in buildGUILogin
		 */
//		try {
//			if(!cc.isConnected()) throw new ConnectionException("Connection Failed");
//			ClientRunnable client = new ClientRunnable();
//			Thread t = new Thread(client);
//			t.start();
//			/*
//		 * It waits for the other Thread to join.
//		 */
//		t.join();
//		} catch(Exception e) {
//			System.out.println(e.getMessage());
//		}
	}
	
	/**
	 * The inner class ClientRunnable is used to receive messages from the server.
	 * The message received can be ORDER or ERROR.
	 *Iit behaves accordingly to the message received:
	 *	- ORDER: it updates the GraphicInterface,
	 *  - ERROR: ti shows a JOptionPane with the error.
	 * @author tom93d
	 *
	 */
	public class ClientRunnable implements Runnable {
		
		private ClientSideMessage clientSideMessage;
		
		@Override
		public void run() {
			try {
				while(true) {
					/*
					 * It exits if the client is not connected
					 */
					if(!cc.isConnected()) System.exit(0);
					clientSideMessage = cc.getNextMessage();
					switch(clientSideMessage.getType()) {
					case ORDER:
						/*
						 * Orders shown on the GUI are those received from the server.
						 */
		
						graphicInterface.update(clientSideMessage.getOrder());
						
						break;
					case ERROR:
						
						JOptionPane.showMessageDialog(null, clientSideMessage.getError(), "ERROR",JOptionPane.INFORMATION_MESSAGE);
						break;
					}
				}
			} catch(Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}

}
